<?php

/**
 * CURL远程请求
 * 
 * @param string $url          请求地址
 * @param array  $data         请求参数
 * @param array  $header       请求头
 * @param bool   $https        协议
 * @return mixed
 */
function curlData($url, $data = [], $header, $https = false){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    //curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//只获取页面内容，但不输出
    if($https){
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//https请求 不验证证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//https请求 不验证HOST
    }
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);      //允许跳转
    curl_setopt($ch, CURLOPT_TIMEOUT, 3000);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //模拟的header头
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	
	//设置代理
	//curl_setopt ($ch, CURLOPT_PROXY, "代理地址");
	//curl_setopt ($ch, CURLOPT_PROXYPORT, "代理端口");
	//curl_setopt ($ch, CURLOPT_PROXYUSERPWD, "代理账号:代理密码");

	
    //curl_setopt($ch, CURLOPT_HEADER, false);//设置不需要头信息
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);//显示请求头信息
    $result = curl_exec($ch);//执行请求
    if (!curl_errno($ch)) {
        print_r(curl_getinfo($ch));
        //显示包含 cURL 请求的 header 信息，与CURLOPT_HTTPHEADER配合使用
    }
    curl_close($ch);//关闭curl，释放资源
    //print_r($result);exit;
    return $result;
}


/**
 * curl批量请求接口
 * @param $data      接口参数
 * @return false
 */
function batchCurl($data){

	$mh = curl_multi_init();

	foreach ($data as $i=>$curlData) {
		$curlUrl=$curlData['url'];
		$conn[$i]=curl_init($curlUrl);
		curl_setopt($conn[$i],CURLOPT_RETURNTRANSFER,1);
		curl_setopt($conn[$i], CURLOPT_POST, 1);
		curl_setopt($conn[$i], CURLOPT_POSTFIELDS, json_encode($curlData['data']));//请求数据
		curl_multi_add_handle ($mh,$conn[$i]);
		curl_setopt($conn[$i], CURLOPT_TIMEOUT, 10);
	}

	do {
		$mrc = curl_multi_exec($mh,$active);
	} while ($mrc == CURLM_CALL_MULTI_PERFORM);

	while ($active and $mrc == CURLM_OK) {
		if (curl_multi_select($mh) != -1) {
			do {
				$mrc = curl_multi_exec($mh, $active);
			} while ($mrc == CURLM_CALL_MULTI_PERFORM);
		}
	}

	foreach ($data as $i=>$curlData) {
		$res[$i]=curl_multi_getcontent($conn[$i]);
		curl_close($conn[$i]);
	}

	if (!$res)   return false;
	return $res;
}

/**
 * curl批量下载远程图片接口
 * @param $data      远程图片访问地址参数
 * @return false
 */
function batchDownLoadPics($data){
	$data = [
		[
			'url' => 'https://fanyi-cdn.cdn.bcebos.com/static/translation/pkg/index_z_b19b64f.png',
		],
		[
			'url' => 'https://fanyi-cdn.cdn.bcebos.com/static/translation/img/header/logo_40c4f13.svg'
		],
		[
			'url' => 'https://fanyi-cdn.cdn.bcebos.com/static/translation/widget/translate/output/img/short-banner1_edeef70.png'
		]
	];
	$mh = curl_multi_init();

	foreach ($data as $i=>$curlData) {
		$fileData = explode('/', $curlData['url']);
		$filename[$i] = pathinfo(array_pop($fileData), PATHINFO_BASENAME);
		$folderName[$i] = '/uploads/images/'.array_pop($fileData).'/';
		if (!is_dir($folderName[$i])) {
			if (!mkdir($folderName[$i], 0777, true))  return new Exception_Service(426, '文件夹uploads创建失败');
		}

		$curlUrl=$curlData['url'];
		$conn[$i]=curl_init($curlUrl);
		curl_setopt($conn[$i], CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
		curl_setopt($conn[$i], CURLOPT_URL, $curlUrl);
		curl_setopt($conn[$i], CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($conn[$i], CURLOPT_CONNECTTIMEOUT, 1);
		curl_multi_add_handle ($mh,$conn[$i]);
	}

	do {
		$mrc = curl_multi_exec($mh,$active);
	} while ($mrc == CURLM_CALL_MULTI_PERFORM);

	while ($active and $mrc == CURLM_OK) {
		if (curl_multi_select($mh) != -1) {
			do {
				$mrc = curl_multi_exec($mh, $active);
			} while ($mrc == CURLM_CALL_MULTI_PERFORM);
		}
	}

	foreach ($data as $i=>$curlData) {
		$res[$i]=curl_multi_getcontent($conn[$i]);
		curl_close($conn[$i]);
		$resource = fopen($folderName[$i] . $filename[$i], 'a');
		fwrite($resource, $res[$i]);
	}

	fclose($resource);
}



curl请求：
curl https://www.xxx.com -X POST -H "Appkey:aaa" -H "Nonce:bbb" -H "Curtime:1640333639" -H "Checksum:ccc" -d "type=aaa&trxamt=100&reqsn=fdsgdsgfgfgf&paytype=W06&body=测试"